<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="btnWrap">
      <div>
        <el-button type="primary" v-hasPermi="['order:contractAssess:export']" @click="handleExport" :loading="exportLoading">
          导出数据
        </el-button>
        <el-button type="primary" v-hasPermi="['order:contractAssess:save']" @click="save"> 保存 </el-button>
      </div>
      <div>
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="list"
      border
      @selection-change="handleSelectionChange"
      :height="tableHeight"
    >
      <el-table-column type="selection" width="55" fixed="left"/>
      <el-table-column
        v-for="column in tableTitleList"
        :key="column.field"
        :label="column.label"
        align="left"
        :width="column.width"
      >
        <template #default="scope">
          <span v-if="column.field === 'jsSort'">{{ scope.$index + 1 }}</span>
          <span v-else-if="column.field === 'salesDepartment'">
            <el-input
              v-nextInput="'salesDepartment'"
              name="salesDepartment"
              v-model="scope.row.salesDepartment"
            />
          </span>
          <span v-else-if="column.field === 'generalManagement'">
            <el-input
              v-nextInput="'generalManagement'"
              name="generalManagement"
              v-model="scope.row.generalManagement"
            />
          </span>
          <span v-else-if="column.field === 'technicalSection'">
            <el-input
              v-nextInput="'technicalSection'"
              name="technicalSection"
              v-model="scope.row.technicalSection"
            />
          </span>
          <span v-else-if="column.field === 'engineeringDepartment'">
            <el-input
              v-nextInput="'engineeringDepartment'"
              name="engineeringDepartment"
              v-model="scope.row.engineeringDepartment"
            />
          </span>
          <span v-else-if="column.field === 'purchasingDepartment'">
            <el-input
              v-nextInput="'purchasingDepartment'"
              name="purchasingDepartment"
              v-model="scope.row.purchasingDepartment"
            />
          </span>
          <span v-else-if="column.field === 'warehouse'">
            <el-input v-nextInput="'warehouse'" name="warehouse" v-model="scope.row.warehouse" />
          </span>
          <span v-else-if="column.field === 'productionDepartment'">
            <el-input
              v-nextInput="'productionDepartment'"
              name="productionDepartment"
              v-model="scope.row.productionDepartment"
            />
          </span>
          <span v-else-if="column.field === 'qualityInspectionDepartment'">
            <el-input
              v-nextInput="'qualityInspectionDepartment'"
              name="qualityInspectionDepartment"
              v-model="scope.row.qualityInspectionDepartment"
            />
          </span>

          <span v-else>{{ scope.row[column.field] }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getList" />
</template>

<script setup lang="ts" name="ContractAssess">
import download from '@/utils/download'
import { useTagsViewStore } from '@/store/modules/tagsView'
import * as ContractAssessApi from '@/api/orderManagement/contractAssess'
const message = useMessage() // 消息弹窗
const tagsViewStore = useTagsViewStore()
const routes = useRouter() // 路由
const { push } = useRouter()
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
let tableTitleList = ref<any>([])
const queryParams = reactive({
  pageNo: 1,
  pageSize: 50,
  orderNumber: null,
  productNumber: null,
  productName: null,
  customerName: null,
  salesDepartment: null,
  generalManagement: null,
  technicalSection: null,
  engineeringDepartment: null,
  purchasingDepartment: null,
  warehouse: null,
  productionDepartment: null,
  qualityInspectionDepartment: null,
  createTime: [],
  creatorName: null
})
const multipleSelection = ref<any>([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
  console.log(multipleSelection.value)
}
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ContractAssessApi.getContractAssessPage(queryParams)
    list.value = data.list
    total.value = data.total
    tableTitleList.value = data.labels
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 保存 */
const save = async () => {
  await ContractAssessApi.updateContractAssess(list.value)
  message.success(t('common.updateSuccess'))
  tagsViewStore.delView(routes.currentRoute.value)
  push('/orderManagement/orderManagement/singleContractBatchManagement/contractAssesslnfo')
}

/** 导出按钮操作 */
const exportRef = ref()
const handleExport = () => {
  exportRef.value.open()
}
//导出数据
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    ids: ''
  }
  if (val.range === 'all') {
    params.ids = ''
  } else {
    multipleSelection.value.map((item) => {
      console.log(item.id)
      params.ids += (item as any).id + ','
    })
  }

  const data = await ContractAssessApi.exportContractAssess(params)
  download.excel(data, val.name)
}

/**打开表格表头设置**/
const tableTitleSet = ref()
const openTableTitleSet = () => {
  const arr = tableTitleList.value
  let code = 'ContractAssess'
  tableTitleSet.value.open(arr, code)
}

/** 初始化 **/
const tableHeight = ref(0)
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
  tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
  getList()
})
</script>
<style lang="scss" scoped>
.btnWrap {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
</style>
